package com.hippo.core.ui.compose.setting

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.ImageBackground
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.theme.ColorBody
import com.hippo.core.ui.theme.ColorContentBg
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.vm.NotificationVM
import kotlinx.serialization.json.JsonNull.content

@Composable
fun MessageSettingScreen(onBack: () -> Unit, vm: NotificationVM = viewModel()) {

    var checked by remember { mutableStateOf(true) }

    LaunchedEffect(Unit) {
        vm.getPushStatus { checked = it }
    }

    CommonBody(
        title = stringResource(R.string.setting_8),
        onBack = onBack
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(ColorContentBg)
                .paddingBody()
        ) {


            Box {

                Image(
                    painter = painterResource(R.mipmap.cardbg),
                    contentDescription = null, // 图片的描述，如果是背景可以设为null
                    contentScale = ContentScale.Crop, // 裁剪图片以适应屏幕
                    modifier = Modifier.fillMaxWidth()
                )

                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 26.dp, vertical = 16.dp)
                ) {
                    Text(
                        text = stringResource(R.string.setting_8),
                        modifier = Modifier.weight(1f),
                        style = MaterialTheme.typography.labelMedium
                    )

                    Switch(
                        checked = checked,
                        onCheckedChange = {
                            Log.i("NotificationVM", "MessageSettingScreen: $it")
                            vm.setPushStatus(it) {
//                            checked = it
                                vm.getPushStatus { checked = it }
                            }
                        },
                    )
                }
            }
        }
    }

}


@Preview(showBackground = true)
@Composable
private fun MessageSettingScreenPreview() {
    HippoTheme {
        MessageSettingScreen({})
    }
}